import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/',
      name: '/',
      component: () => import('../components/Login'),
      meta: {
        title: '登录界面'
      }
    },
    {
      path: '/main',
      name: 'main',
      component: () => import('../components/Main'),
      meta: {
        title: '后台管理'
      },
      children: [
        {
          path: '/user',
          name: 'user',
          component: () => import('../components/UserList'),
          meta: {
            title: '用户管理'
          }
        },
        {
          path: '/seckill',
          name: 'seckill',
          component: () => import('../components/seckillList'),
          meta: {
            title: '秒杀管理'
          }
        },
          {
              path: '/ad',
              name: 'ad',
              component: () => import('../components/Ad'),
              meta: {
                  title: 'ad'
              }
          },
          {
              path: '/demo',
              name: 'demo',
              component: () => import('../components/Demo'),
              meta: {
                  title: 'demo品牌管理'
              }
          },
      ]
    }
  ]
})
router.beforeEach((to, from, next) => {
  document.title = to.meta.title
  if (to.fullPath === '/' || to.fullPath === '/main' || sessionStorage.getItem('token')) {
    next()
  } else {
    console.log('未登录')
    next({name: '/'})
  }
})
export default router
